#!/usr/bin/env python

import gobject, gtk
import math
#from datetime import datetime
import time
import numpy as np
from matplotlib.figure import Figure
# uncomment to select /GTK/GTKAgg/GTKCairo
#from matplotlib.backends.backend_gtk import FigureCanvasGTK as FigureCanvas
from matplotlib.backends.backend_gtkagg import FigureCanvasGTKAgg as FigureCanvas
#from matplotlib.backends.backend_gtkcairo import FigureCanvasGTKCairo as FigureCanvas

import pat.utils   as pu

class PlotGuy(gtk.Frame):
    def __init__(self, title="", ylab=None, ncurve=1, dy_min=0.01):
        self.ncurve = ncurve
        self.dy_min = dy_min
        gtk.Frame.__init__(self)
#        gtk.Frame.__init__(self, title)
#        f = Figure(figsize=(5,3), dpi=100)
        f = Figure()
        self.axis = f.add_subplot(111)
        self.axis.set_title(title)
        if ylab: self.axis.yaxis.set_label_text(ylab)
        self.max_data = 1000
        self.time = np.zeros(self.max_data)
        self.data = np.zeros((self.max_data, ncurve))
        self.nscroll = 600
        self.dt_scroll = 45.
        self.reset_plot()
        self.canvas = FigureCanvas(f)
        self.add(self.canvas)
        self.set_size_request(200, 400)

           


    def reset_plot(self):
        self.ndata = 0
        self.plot_data = [self.axis.plot(self.time[0:0], self.data[0:0,i])[0] for i in range(0, self.ncurve)]
#        self.axis.set_xlim((0., self.dt_scroll))
#        self.axis.set_ylim((-45., 45.))
        


    def update_plot(self):
        if self.ndata == 0: return
        for i, plot in enumerate(self.plot_data):
            plot.set_xdata(self.time[0:self.ndata])
            plot.set_ydata(self.data[0:self.ndata,i])
        self.axis.set_xlim((self.time[0], self.time[0]+self.dt_scroll))
        y_min, y_max = np.min(self.data[0:self.ndata,0]), np.max(self.data[0:self.ndata,0])
        margin = 0.1
        dy = (1.+margin)*(y_max - y_min)
        y_min -= dy*margin/2; y_max += dy*margin/2
        if dy < self.dy_min:
            cor = (self.dy_min-dy)/2; y_min -= cor; y_max += cor
        self.axis.set_ylim((y_min, y_max))
        self.canvas.draw()

        


    def add_data(self, time, data):
        if self.ndata >= self.max_data:
            print "scrolling plot full"
            self.time[0:self.ndata-self.nscroll] = self.time[self.nscroll:self.ndata]
            self.data[0:self.ndata-self.nscroll] = self.data[self.nscroll:self.ndata]
            self.ndata -= self.nscroll
        self.time[self.ndata] = time
        self.data[self.ndata] = data
        self.ndata += 1




#
# This is a test program that plots a sine and a cos
#
def periodic_callback_fast(sc):
    now = time.time()
    val = np.array([math.sin(now), math.cos(now)])
    sc.add_data(now, val)
    return True

def periodic_callback_slow(sc):
    sc.update_plot()
    return True

if __name__ == "__main__":
    window = gtk.Window(gtk.WINDOW_TOPLEVEL)
    sc = PlotGuy(ncurve=2, title="plot")
    window.add(sc)
    window.set_size_request(800, 600)
    window.show_all()
    gobject.timeout_add(int(1000./25.), periodic_callback_fast, sc)
    gobject.timeout_add(int(1000./5.), periodic_callback_slow, sc)
    gtk.main()
